//
//  UITableView+Datasource.m
//  horse
//
//  Created by peter on 15/8/2.
//  Copyright (c) 2015年 soho. All rights reserved.
//


#import "UITableView+Datasource.h"
#import <objc/runtime.h>

const static char kDataArrayAddress;
const static char kPageAddress;
const static char kTotalAddrsss;
const static char kProcessDataSourceCallBackAddress;
const static char kBigStartAddress;
const static char kSingleStartAddress;

@interface UITableView ()

@end

@implementation UITableView (Datasource)


- (void)setProcessDataSourceCallBack:(void (^)(UITableView *))processDataSourceCallBack {
    objc_setAssociatedObject(self, &kProcessDataSourceCallBackAddress, processDataSourceCallBack, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void(^)(UITableView *))processDataSourceCallBack {
    return objc_getAssociatedObject(self, &kProcessDataSourceCallBackAddress);
}

- (void)setTotals:(int)totals {
    objc_setAssociatedObject(self, &kTotalAddrsss, @(totals), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (int)totals {
    return [objc_getAssociatedObject(self, &kTotalAddrsss) intValue];
}

- (void)insertToDataSourceWithArray:(NSArray *)array atIndex:(NSUInteger)index {
    if([array count] == 0) {
        return;
    }
    if(self.dataArray == nil) {
        self.dataArray = [NSMutableArray arrayWithArray:array];
        [self reloadData];
        return;
    }
    if([self.dataArray count] < index) {
        assert(@"数组越界");
        return;
    }
    [self.dataArray insertObjects:array atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(index, [array count])]];
    NSMutableArray *indexPaths = [NSMutableArray array];
    for (int aIndex = 0; aIndex < array.count; ++aIndex) {
        [indexPaths addObject:[NSIndexPath indexPathForRow:index + aIndex inSection:0]];
    }
    [self beginUpdates];
    [self insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
    [self endUpdates];
}

- (void)addToDataSourceWithArray:(NSArray *)array {
    NSMutableArray *indexPaths = [NSMutableArray array];
    if(!array) {
        if(self.dataArray != nil) {
            self.dataArray = nil;
        }
        goto ProcessDataCallBack;
    }
    else if([array count] == 0) {
        if(self.dataArray == nil) {
            self.dataArray = [NSMutableArray array];
        }
        goto ProcessDataCallBack;
    }
    if(self.dataArray == nil) {
        self.dataArray = [NSMutableArray arrayWithArray:array];
        [self reloadData];
        goto ProcessDataCallBack;
    }
    for (int index = 0; index < array.count; ++index) {
        [indexPaths addObject:[NSIndexPath indexPathForRow:[self.dataArray count] + index inSection:0]];
    }
    [self.dataArray addObjectsFromArray:array];
    [self beginUpdates];
    [self insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
    [self endUpdates];
    ProcessDataCallBack:{
        if(self.processDataSourceCallBack) {
            self.processDataSourceCallBack(self);
        }
        if(!self.isShowNoDataView) {
            return;
        }
    }
}

- (NSMutableArray *)dataArray {
    return objc_getAssociatedObject(self, &kDataArrayAddress);
}

- (void)setDataArray:(NSMutableArray *)dataArray {
    objc_setAssociatedObject(self, &kDataArrayAddress, dataArray, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self reloadData];
}

- (int)page {
    return [objc_getAssociatedObject(self, &kPageAddress) intValue];
}

- (void)setPage:(int)page {
    objc_setAssociatedObject(self, &kPageAddress, @(page), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (int)bigStart {
    return [objc_getAssociatedObject(self, &kBigStartAddress) intValue];
}

- (void)setBigStart:(int)bigStart {
    objc_setAssociatedObject(self, &kBigStartAddress, @(bigStart), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (int)singleStart {
    return [objc_getAssociatedObject(self, &kSingleStartAddress) intValue];
}

- (void)setSingleStart:(int)singleStart {
    objc_setAssociatedObject(self, &kSingleStartAddress, @(singleStart), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)scrollToBottom {
    
    NSInteger bottomSection = MAX([self.dataSource numberOfSectionsInTableView:self] - 1,0),
    bottomRow = MAX([self.dataSource tableView:self numberOfRowsInSection:bottomSection] - 1,0);
    if(bottomRow == 0){
        return;
    }
    [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:bottomRow inSection:bottomSection] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

- (void)scrollToUp {
    [self layoutIfNeeded];
    [self setContentOffset:CGPointMake(0, 0) animated:YES];
    
}

@end
